# zope imports
from zope.interface import implements
from zope.component import adapts

# EasyArticle imports
from easyarticle.core.interfaces import IData 
from easyarticle.core.interfaces import IEasyArticle
from easyarticle.core.interfaces import IObjectsManagement

class EasyArticleObjectsManagement(object):
    """Adapter which provide methods to manage sub objects of easyarticle
    content objects.
    """
    implements(IObjectsManagement)
    adapts(IEasyArticle)

    def __init__(self, context):
        """        
        """
        self.context = context
        
    def getObjects(self, portal_types=None, sort_on="getObjPositionInParent"):
        """
        """
        # Note: Can't use here the catalog, as the sub objects are all
        # unindexed.
        
        # Note: It seems that objectValues() provides the objects by default
        # the order of getObjPositionInParent, but to be sure I have to check
        # this.
        
        if portal_types is not None:
            objects = self.context.objectValues(portal_types)
        else:
            objects = self.context.objectValues()
        
        return objects
        
    def getObjectsAsDict(self, portal_types=None):
        """
        """
        result = []
        for object in IObjectsManagement(self.context).getObjects(portal_types):
            
            # Get the object relevant data here. Developers can provide own Data
            # adapter for her content objects.
            data = IData(object).getData()
            result.append(data)
        
        return result